import * as types from './mutation-types'

const state = {
  teachers: null,
  gradStudents: null,
  unGradStudents: null,
  audits: null,
  tabIsGrad: false
}

const mutations = {
  [types.LOAD_TEACHER_ADMIN] (_state, data) {
    _state.teachers = data
  },
  [types.LOAD_GRAD_STUDENT_ADMIN] (_state, data) {
    _state.gradStudents = data
  },
  [types.LOAD_UNGRAD_STUDENT_ADMIN] (_state, data) {
    _state.unGradStudents = data
  },
  [types.LOAD_AUDIT_ADMIN] (_state, data) {
    _state.audits = data
  },
  [types.UPDATE_TAB_GRAD] (_state, data) {
    _state.tabIsGrad = data
  }
}

const actions = {
  updateAdminTaskStatus ({ commit, state }, { taskId, studentId, status }) {
    var data:any = state.audits

    var student = data.items.find((e) => {
      return e.studentId === studentId
    })

    var task = student.tasks.find(e => e.id === taskId)

    var index = student.tasks.indexOf(task)
    student.tasks.splice(index, 1)
  },
  async getTeacherAdmin ({ commit, state }) {
    var data = await types.loadData(state, 'teachers', 'teacher')
    commit(types.LOAD_TEACHER_ADMIN, data)
  },
  async getGradStudentAdmin ({ commit }) {
    var data = await types.loadData(state, 'gradStudents', 'student/byAdmin', { isGrad: true })
    commit(types.LOAD_GRAD_STUDENT_ADMIN, data)
  },
  async getUnGradStudentAdmin ({ commit }) {
    var data = await types.loadData(state, 'unGradStudents', 'student/byAdmin', { isGrad: false })
    commit(types.LOAD_UNGRAD_STUDENT_ADMIN, data)
  },
  async getAuditAdmin ({ commit }) {
    var data = await types.loadData(state, 'audits', 'student/auditList')
    commit(types.LOAD_AUDIT_ADMIN, data)
  },

  updateTabIndex ({ commit }, index) {
    if (index === 0) {
      commit(types.UPDATE_TAB_GRAD, false)
    } else if (index === 1) {
      commit(types.UPDATE_TAB_GRAD, true)
    }
  }
}

const getters = {

}

export default {
  state,
  getters,
  actions,
  mutations
}
